
/*
 * Copyright (C) 2009 Archie L. Cobbs. All rights reserved.
 *
 * $Id: XMLSimpleField.java 317 2010-02-11 22:06:51Z archie.cobbs $
 */

package org.dellroad.sidekar.xml;

import org.dellroad.sidekar.SimpleField;

/**
 * Container for a simple field's value during the Sidekar XML import/export process.
 */
abstract class XMLSimpleField<T> extends XMLField {

    // Used during unmarshalling (for fields of items)
    protected XMLSimpleField(XMLItem xmlItem) {
        super(xmlItem);
    }

    // Used during unmarshalling (for nested fields)
    protected XMLSimpleField(XMLField xmlField) {
        super(xmlField.getXMLItem(), xmlField.getNestedField());
    }

    // Used during marshalling
    protected XMLSimpleField(String name) {
        super(name);
    }

    /**
     * This implementation compares the result of {@link #getObjectValue} to null.
     */
    // Used during marshalling
    @Override
    protected boolean hasDefaultValue() {
        return getObjectValue() == null;
    }

    /**
     * Simple fields cannot have nested fields, so this implementation always throws {@link IllegalArgumentException}.
     */
    @Override
    protected SimpleField<?> getNestedField() {
        throw new IllegalArgumentException("simple fields don't have nested fields");
    }

    // Used during unmarshalling (for elements contained in arrays and complex fields)
    public abstract T getObjectValue();
}

